package com.man.control;

import java.io.IOException;
import java.util.ArrayList;
import java.sql.Connection;
import java.sql.Date;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.List;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.apache.struts2.ServletActionContext;
import org.apache.struts2.json.JSONException;
import org.apache.struts2.json.JSONUtil;

import com.man.daoService.DaoService;
import com.man.entity.Friends;
import com.man.entity.User;

public class DateAction {
	private DaoService daoService=new DaoService();
	public String insert()
	{
		User user=(User) ServletActionContext.getRequest().getSession().getAttribute("user");
		 com.man.entity.Date date=new com.man.entity.Date();
		 date.setUserName(user.getUserName());
		 date.setDate(getDate());
		 date.setThing(getThing());
		 boolean b=	 daoService.dateInsert(date);
		 return "insert";
//		try {
//			ServletActionContext.getResponse().getWriter().print(b);
//		} catch (IOException e) {
//			// TODO Auto-generated catch block
//			e.printStackTrace();
//		}
	}
	public void select()
	{User user=(User) ServletActionContext.getRequest().getSession().getAttribute("user");
	 com.man.entity.Date date=new com.man.entity.Date();
	 date.setUserName(user.getUserName());
	 List<com.man.entity.Date> list= daoService.dateSelect(date);
	 try {
		String s= JSONUtil.serialize(list);
		ServletActionContext.getResponse().setCharacterEncoding("utf-8");
		ServletActionContext.getResponse().getWriter().print(s);
	} catch (JSONException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	} catch (IOException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}
	
	}
	public void oneSelect()
	{	
	 User user=(User) ServletActionContext.getRequest().getSession().getAttribute("user");
	 com.man.entity.Date date=new com.man.entity.Date();
	 date.setUserName(user.getUserName());
	 date.setDate(getDate());
	 com.man.entity.Date date2= daoService.dateOneSelect(date);
	 JSONObject jsonObject=JSONObject.fromObject(date2);	
	 ServletActionContext.getResponse().setCharacterEncoding("utf-8");
		try {
			ServletActionContext.getResponse().getWriter().print(jsonObject);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}	 
	}
	public void delect()
	{	
		User user=(User) ServletActionContext.getRequest().getSession().getAttribute("user");
		 com.man.entity.Date date=new com.man.entity.Date();
		date.setUserName(user.getUserName());
		date.setDate(getDate());
		Boolean b=daoService.dateDelect(date);
		try {
			ServletActionContext.getResponse().getWriter().print(b);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	public void updata()
	{	
		User user=(User) ServletActionContext.getRequest().getSession().getAttribute("user");
		 com.man.entity.Date date=new com.man.entity.Date();
			date.setUserName(user.getUserName());
			date.setDate(getDate());
			date.setThing(getThing());
		Boolean b=daoService.dateUpdata(date);
		try {
			ServletActionContext.getResponse().getWriter().print(b);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	
	
	private String userName ;
	public String getUserName() {
		return userName;
	}
	public void setUserName(String userName) {
		this.userName = userName;
	}
	public String getDate() {
		return date;
	}
	public void setDate(String date) {
		this.date = date;
	}
	public String getThing() {
		return thing;
	}
	public void setThing(String thing) {
		this.thing = thing;
	}
	private  String date;
	private String thing;

}
